package portal.dao;

import beans.BaseResourceState;
import dao.PublicDao;
import dao.Query;
import dao.QueryCallBackList;
import net.sf.json.JSONObject;
import portal.mybatis.dao.service.ClusterHostService;
import portal.mybatis.page.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ScheduleClusterHostDao {
	@Autowired
	ClusterHostService clusterHostService;
    // 插入集群资源情况
    public boolean insertHostDetail(BaseResourceState baseResource){
    	return clusterHostService.insertHostDetail(baseResource);
    }
    // 查询节点名称是否已经存在 By黄振
    @Deprecated
    public boolean isHostByHostName1(String hostName) {
        String sql = "select count(*) from schedule_cluster_host where host_name = ?";
        Object[] params = new Object[]{hostName};
        return PublicDao.oneNum(sql, params) > 0;
    }
    public boolean isHostByHostName(String hostName){
    	return clusterHostService.isHostNameExist(hostName);
    }
    //查询集群资源情况
    /**
     * 查询所有Host By 黄振
     * @param userId not used
     * @param onePageCount
     * @param currentPage
     * @return
     */
    public List<Object> getClusterHosts(String userId, Integer onePageCount, Integer currentPage) {
    	return clusterHostService.getClusterHosts(currentPage,onePageCount);
    }
    
    /**
     * 根据每页数量计算总页数 By黄振
     *
     * @param onePageCount
     * @return
     */
    public Long getHostsPageNum(Integer onePageCount) {
        Long hostNum = this.getHostsNum();
        return (hostNum % onePageCount) == 0 ? (hostNum / onePageCount) : (hostNum / onePageCount + 1);
    }

    /**
     * 获取所有的节点数量 By黄振
     *
     * @return
     */
    public synchronized Long getHostsNum() {
        //String sql = "select count(*) from schedule_cluster_host GROUP BY host_name ";
        return clusterHostService.countHosts();
    }

    /**
     * 查询所有Host名称查询Host By 黄振
     */
    public List<Object> searchHostByHostName(String hostName,Integer onePageCount, Integer currentPage) {
    	if(currentPage<1)
    		currentPage=1;
    	return clusterHostService.selectPageHosts(currentPage*onePageCount -onePageCount, onePageCount, hostName);
    }
    
    /**
     * 根据节点名称和每页数量计算总页数 By黄振
     *
     * @param onePageCount
     * @return
     */
    public long getHostsPageNumByHostName(Integer onePageCount,String hostName) {
    	return clusterHostService.countPageHosts(new Page<BaseResourceState>(onePageCount,onePageCount,hostName));
    }
    /**
     * 根据节点名称查询节点最近7天历史信息  By 黄振
     */
    public List<Object> getHostHistoryState(String hostName) {
    	return clusterHostService.selectHisByHost(hostName);
    }
}
